package com.zjw.zy.utils;

import android.content.Intent;
import android.text.TextUtils;

import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.AccessToken;
import com.baidu.ocr.sdk.model.IDCardParams;
import com.baidu.ocr.sdk.model.IDCardResult;
import com.zjw.zy.App;
import com.zjw.zy.constant.AppConstant;
import com.zjw.zy.smi.JustWithIt;
import com.zjw.zy.smi.JustWithIt2;

import java.io.File;

import ocr.ocr.ui.camera.CameraActivity;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/19 12:02
 */
public final class OcrUtils {

    private static final String TAG = "OcrUtils";

    private static final int CRAMER_REQUEST_CODE = "CRAMER_REQUEST_CODE".hashCode();

    private static boolean isAccess = false;

    public static void init(App app) {
        OCR.getInstance(app)
                .initAccessTokenWithAkSk(new OnResultListener<AccessToken>() {
                    @Override
                    public void onResult(AccessToken accessToken) {
                        LogUtils.d(TAG, "init success");
                        String token = accessToken.getAccessToken();
                        isAccess = true;
                    }

                    @Override
                    public void onError(OCRError ocrError) {
                        LogUtils.e(TAG, ocrError.getMessage());
                    }
                }, app, AppConstant.getBdAk(), AppConstant.getBdSk());
    }

    public static boolean isAccess() {
        return isAccess;
    }

    public static void startCramer() {
        AppUtils.getMainActivity().observerActivityResult(CRAMER_REQUEST_CODE, (result, data) -> {

        });

        Intent intent = new Intent(AppUtils.getMainActivity(), CameraActivity.class);

        AppUtils.getMainActivity().startActivityForResult(intent, CRAMER_REQUEST_CODE);
    }


    public static void startIdentityCramer(IDOCRListener call) {
        AppUtils.getMainActivity().observerActivityResult(CRAMER_REQUEST_CODE, (result, data) -> {
            if (data == null) {
                LogUtils.e(TAG,"startIdentityCramer data == null");
                return;
            }
            String contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
            String filePath = PathUtils.genericOrcImageFilePath();
            if (!TextUtils.isEmpty(contentType)) {
                call.onStart();
                recIDCard(filePath, call);
            } else {
                call.onError();
            }
        });

        Intent intent = new Intent(AppUtils.getMainActivity(), CameraActivity.class);
        intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                PathUtils.genericOrcImageFilePath());

        intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
        AppUtils.getMainActivity().startActivityForResult(intent, CRAMER_REQUEST_CODE);
    }


    private static void recIDCard(String filePath, IDOCRListener call) {
        IDCardParams param = new IDCardParams();
        param.setImageFile(new File(filePath));
        // 设置身份证正反面
        param.setIdCardSide(IDCardParams.ID_CARD_SIDE_FRONT);
        // 设置方向检测
        param.setDetectDirection(true);
        // 设置图像参数压缩质量0-100, 越大图像质量越好但是请求时间越长。 不设置则默认值为20
        param.setImageQuality(20);

        param.setDetectRisk(true);

        OCR.getInstance(AppUtils.getMainActivity()).recognizeIDCard(param, new OnResultListener<IDCardResult>() {
            @Override
            public void onResult(IDCardResult result) {
                call.onResult(result);
            }

            @Override
            public void onError(OCRError error) {
                LogUtils.e(TAG, error.getMessage());
                call.onError();
            }
        });
    }


    public interface IDOCRListener {
        void onStart();

        void onError();

        void onResult(IDCardResult result);
    }
}
